
PDF 檔案通常由 14 種標準字體組成,但有時我們會發現需要使用自訂字體來更好地呈現文字。自訂字體可以與常規文字、浮水印戳、頁首/頁尾或表格物件一起使用。您可以在整個文件中使用單一字體,也可以考慮為每個物件使用單獨的字體(即使同一類型的個別物件也可能具有獨特的字體)。為了支援此功能,Aspose.PDF Cloud 的最新版本已升級以支援此功能。
PDF處理API
Aspose.PDF Cloud提供建立和操作 PDF 檔案的功能。由於 API 是根據 REST 架構開發的,因此我們可以在任何平台上使用該 API,例如 Windows、Linux、macOS。此外,您還可以在各種應用程式中使用我們的 API,包括桌面、行動、Web 等。現在開始使用 Cloud SDKs。第一步是安裝它們,相關詳細資訊可以參考如何安裝 Aspose.Cloud SDKs。
自訂字體的文本
Aspose.PDF Cloud 的最新版本支援在向 PDF 檔案新增文字時引用自訂字體的功能。
捲曲
curl -X PUT "https://api.aspose.cloud/v3.0/pdf/MyNewFile.pdf/pages/1/text" -H "accept: application/json" -H "authorization: Bearer <JWT Token>" -H "Content-Type: application/json" -H "x-aspose-client: Containerize.Swagger" -d "{ \"LineSpacing\": \"FontSize\", \"WrapMode\": \"ByWords\", \"HorizontalAlignment\": \"FullJustify\", \"LeftMargin\": 10, \"RightMargin\": 10, \"TopMargin\": 20, \"BottomMargin\": 20, \"Rectangle\": { \"LLX\": 100, \"LLY\": 600, \"URX\": 300, \"URY\": 200 }, \"Rotation\": 10, \"SubsequentLinesIndent\": 10, \"VerticalAlignment\": \"Center\", \"Lines\": [ { \"HorizontalAlignment\": \"Left\", \"Segments\": [ { \"Value\": \"Hello World..\", \"TextState\": { \"FontSize\": 18, \"Font\": \"Arial\", \"ForegroundColor\": { \"A\": 0x00, \"R\": 0x33, \"G\": 0x33, \"B\": 0x99 }, \"BackgroundColor\": { \"A\": 0x00, \"R\": 0xCC, \"G\": 0xFF, \"B\": 0xCC }, \"FontStyle\": \"Regular\", \"FontFile\": \"Allura-Regular.otf\" } } ] } ]}"
請求 URL
https://api.aspose.cloud/v3.0/pdf/MyNewFile.pdf/pages/1/text
C#.NET
// 完整範例和資料檔請前往https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-dotnet
PdfApi pdfApi = new PdfApi("API_KEY", "APP_SID");
string Name = "MyNewFile.pdf";
try {
var response = api.PutAddText(Name, 1, PutAddTextExample());
Console.WriteLine(response);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
}
public static Aspose.Pdf.Cloud.Sdk.Model.Paragraph PutAddTextExample()
{
//ExStart: PutAddText範例
Aspose.Pdf.Cloud.Sdk.Model.Paragraph paragraph = new Aspose.Pdf.Cloud.Sdk.Model.Paragraph(
Rectangle: new Aspose.Pdf.Cloud.Sdk.Model.Rectangle(100, 600, 300, 800),
LeftMargin: 10,
RightMargin: 10,
TopMargin: 10,
BottomMargin: 10,
HorizontalAlignment: Aspose.Pdf.Cloud.Sdk.Model.TextHorizontalAlignment.FullJustify,
LineSpacing: Aspose.Pdf.Cloud.Sdk.Model.LineSpacing.FontSize,
Rotation: 10,
SubsequentLinesIndent: 10,
VerticalAlignment: Aspose.Pdf.Cloud.Sdk.Model.VerticalAlignment.Center,
WrapMode: Aspose.Pdf.Cloud.Sdk.Model.WrapMode.ByWords,
Lines: new System.Collections.Generic.List<Aspose.Pdf.Cloud.Sdk.Model.TextLine>
{
new Aspose.Pdf.Cloud.Sdk.Model.TextLine(
HorizontalAlignment: Aspose.Pdf.Cloud.Sdk.Model.TextHorizontalAlignment.Right,
Segments: new System.Collections.Generic.List<Aspose.Pdf.Cloud.Sdk.Model.Segment>
{
new Aspose.Pdf.Cloud.Sdk.Model.Segment(
Value: "Aspose.PDF Cloud API",
TextState: new Aspose.Pdf.Cloud.Sdk.Model.TextState(
Font: "Arial",
FontSize: 16,
ForegroundColor: new Aspose.Pdf.Cloud.Sdk.Model.Color(0x00, 0x33, 0x33, 0x99),
// 背景顏色:全新的 Aspose.Pdf.Cloud.Sdk.Model.Color(0x00, 0xCC, 0xFF, 0xCC),
FontStyle: Aspose.Pdf.Cloud.Sdk.Model.FontStyles.BoldItalic, FontFile: "Allura-Regular.otf"
)
)
}
)
}
);
return paragraph;
}

圖 1:帶有自訂字體的文字預覽。
文字替換時使用自訂字體
雲端 API 提供了搜尋文字並將其替換為新文字段的功能。在文字替換期間,我們可以提供更改字體格式的選項,包括新的字體檔案。
cURL 指令
curl -X POST "https://api.aspose.cloud/v3.0/pdf/MyNewFile.pdf/pages/1/text/replace" -H "accept: application/json" -H "authorization: Bearer <JWT Token>" -H "Content-Type: application/json" -H "x-aspose-client: Containerize.Swagger" -d "{ \"TextReplaces\": [ { \"OldValue\": \"API\", \"NewValue\": \".NET SDK\", \"Regex\": true, \"TextState\": { \"FontSize\": 12, \"Font\": \"Arial\", \"ForegroundColor\": { \"A\": 0x00, \"R\": 0x33, \"G\": 0x33, \"B\": 0x99 }, \"BackgroundColor\": { \"A\": 0x00, \"R\": 0xCC, \"G\": 0xFF, \"B\": 0xCC }, \"FontStyle\": \"Regular\", \"FontFile\": \"KaushanScript-Regular.otf\" }, \"Rect\": { \"LLX\": 0, \"LLY\": 0, \"URX\": 0, \"URY\": 0 } } ], \"DefaultFont\": \"string\", \"StartIndex\": 0, \"CountReplace\": 0}"
請求 URL
https://api.aspose.cloud/v3.0/pdf/MyNewFile.pdf/pages/1/text/replace
C#.NET
// 完整範例和資料檔請前往https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-dotnet
Aspose.Pdf.Cloud.Sdk.Api.PdfApi api = new Aspose.Pdf.Cloud.Sdk.Api.PdfApi("API_KEY", "APP_SID");
public static void PostPageTextReplaceExample()
{
//ExStart:PostPageTextReplace範例
var rect = new Aspose.Pdf.Cloud.Sdk.Model.Rectangle(100, 600, 300, 800);
var textState = new Aspose.Pdf.Cloud.Sdk.Model.TextState(
FontSize: 14, ForegroundColor: new Aspose.Pdf.Cloud.Sdk.Model.Color(
0x00, 0x33, 0x13, 0x49), FontFile: "KaushanScript-Regular.otf");
var textReplace = new Aspose.Pdf.Cloud.Sdk.Model.TextReplace("API", "SDK", true, TextState: textState , Rect: rect);
var textReplaceList = new Aspose.Pdf.Cloud.Sdk.Model.TextReplaceListRequest(
new System.Collections.Generic.List<Aspose.Pdf.Cloud.Sdk.Model.TextReplace> { textReplace },
StartIndex: 0, CountReplace: 1);
Aspose.Pdf.Cloud.Sdk.Api.PdfApi api = new Aspose.Pdf.Cloud.Sdk.Api.PdfApi("15fa9268fd293c7998a5051c88a75f80", "265ae48d-aa27-4470-9e84-3e383050a436");
var response = api.PostPageTextReplace("MyNewFile.pdf", 1, textReplaceList);
Console.WriteLine(response);
//擴充結束:PostPageTextReplace範例
}

圖 2:使用自訂字體的文字取代預覽。
自訂字體也可以用於頁首/頁尾、表格和圖章物件。
結論
本文說明如何在 PDF 中新增文字時使用自訂字體以及如何在 PDF 文件中取代現有文字時使用自訂字體的步驟。除了上面提到的功能之外,您還可以探索程式設計師指南中解釋的大量其他令人驚嘆的功能。此外,請注意,我們的雲端 API 是在 MIT 許可下發布的,其完整的原始程式碼可以從 GitHub 下載。
如果您在使用 API 時遇到任何問題,請透過免費的客戶支援服務聯絡我們。
相關文章
我們建議您訪問以下連結以了解更多資訊: